// pages/view/cl/drugStatistics/drugStatistics.js
const network = require('../../../../utils/network.js')
var app = getApp();
var pageSize = 0;
var startTime_n;
var endTime_n;
var statisticType_n;
var order_n;
Page({

  /**
   * 页面的初始数据
   */
  data: {

    headers: [{ text: 'rowNum', display: '排名' }, { text: 'itemCode', display: '药品代码' },
      { text: 'itemName', display: '药品名称' }, { text: 'standard', display: '规格' },
      { text: 'unitName', display: '单位' }, { text: 'quantity', display: '数量' },
      { text: 'amount', display: '金额' }],
      row: [],
      current: 1,
      totalPage: 1,
      startTime:'',
      endTime:'',
      myTitle:''
  },

  onChange(e) {
    console.log('onChange', e)
    this.setData({
      current: e.detail.current,
    })

    this.myRequest(e)
  },

 
  /**
 * 调用封装好的网络请求，获取数据
 */
  myRequest: function (options) {
    var that = this;
    //console.log('options:', options)
    //获得表格高度
    var windowheight = wx.getSystemInfoSync().windowHeight;
    var tdHeight = [18.4, 21.6, 39]
    if (windowheight < 550) {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[0]);
    } else if (windowheight < 850) {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[1]);
    } else {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[2]);
    }
    console.log("每页个数", pageSize);
    //调用封装好的网络请求，获取数据
    network.get('/cl/drugstatistics?startTime=' + startTime_n + '&endTime=' + endTime_n + '&statisticType=' + statisticType_n + '&order=' + order_n + '&pageCount=' + that.data.current + '&pageSize=' + pageSize, {}).then(function (res) {
      console.log('res:', res)

      that.setData({
        startTime: startTime_n,
        endTime: endTime_n,
        msg: res.data.errMsg,
        row: res.data.data,
        totalPage: res.data.totalPage
      })
      console.log('row:', that.data.row)

    }).then(function (res) {
      wx.hideLoading()

    }).catch(function (res) {
      console.log(res)
    })
  },

  getTitle: function (statisticType_n){
   var that=this;
    //console.log('statisticType_n', statisticType_n)
    if (statisticType_n==1){
      that.setData({
        myTitle: '门诊药品消耗'
      })
    
    }else if (statisticType_n==2)
    {
      that.setData({
        myTitle: '门诊国产药消耗'
      })
    
    } else if (statisticType_n == 3){
      that.setData({
        myTitle: '门诊进口药消耗'
      })
      
    } else{
      that.setData({
        myTitle: '门诊生素消耗'
      })
    
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    console.log('options:', options)
    //保存参数值，分页使用
    startTime_n = options.startTime
    endTime_n = options.endTime
    statisticType_n = options.statisticType;
    order_n = options.order;
    
    that.myRequest(options)
    that.getTitle(statisticType_n)

    //调用封装好的网络请求，获取数据
    // network.get('/cl/drugstatistics?startTime=' + options.startTime + '&endTime=' + options.endTime + '&statisticType=' + options.statisticType + '&order=' + options.order,{}).then(function (res) {
    //   console.log('res:', res)
    //   var obj = res.data.data
    //   that.setData({
    //     'drug_list': obj,
    //   })
    // }).then(function (res) {

    // }).catch(function (res) {
    //   console.log(res)
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})